/** Trigger Name: OORTelesalesAssociateCampaignOnLead 
 * Author: Accenture
 * Date: 20-March-2012
 * Requirement/Project Name: OORTelesales
 * Requirement Description: Stamp Campaign Id on Leads via Campaign Members.
 * Bhumika Udani    v1.1  	 2 May 2012		         included owner updates as per 798 defect
 * Bhumika Udani    v1.1     10 May 2012             Implemented requirement 1455
 * Bhumika Udani    v1.1     15 May 2012             Implemented dedup campaign logic as per requirement 1492
 * Kunal Sharma     v1.1     22 May 2012             Condition added for Unchecking Lead Temp check Box on removing the campaign member.
 */
trigger OORTelesalesAssociateCampaignOnLead on CampaignMember (after Insert,after Update,after Delete){
    // Set for Storing Updated Leads with Campaingn Id.
    set<CampaignMember> camMemList = new set<CampaignMember>();
    map<string, string> ypstringCampaignMemIdMap = new map<string, string>();   
    
    set<string> ypComstringSet = new set<string>();
    list<string> dupCamMembIdList = new list<string>();
    
    public ID camTypeID = RecordTypeHelper.GetRecordTypeIdByDevName('Campaign', 'OOR_Telesales_Campaign');

    if(trigger.isInsert || trigger.isUpdate){

        for(CampaignMember campMemb:Trigger.New){ 
          
            system.debug('This member is being inserted ' +campMemb);
        
        // Check if lead and campaignid are populated
            if(campMemb.LeadId != null && campMemb.CampaignId != null ){

        // Check Lead source should not be null
                if(campMemb.LeadSource__c!= null){
                        camMemList.add(campMemb);                                 
                       }                    
               else{
                 campMemb.addError(OORTelesalesTextConstant.LEAD_SOURCE_CANNOT_BE_BLANK);   
               }
           }
       }
       if(camMemList.size()>0 ){
           system.debug('This is the list for dedup check '+camMemList );
            OORTelesalesTriggerHandler.duplicateLeadCampaignMemberCheck(camMemList);
        }
    }
   else if(trigger.isDelete){
   
   ID localAdvertiserRecordTypeID = RecordTypeHelper.GetRecordTypeIdByDevName('CampaignMember','OORTelesales'); 
   
   system.debug(localAdvertiserRecordTypeID) ;
   
    
   List<Lead> leadList = new List<Lead>();//List for storing Leads which are related with removed campaign members.   
   List<Id> idList= new List<Id>(); //List for storing ids of removed leads
   
     
   for(CampaignMember campMemb: Trigger.Old){
           idList.add(campMemb.LeadId);
   }
   
   leadList = [SELECT Id, TemporaryOwner__c,OORTelesalesCampaign__c, OwnerId FROM Lead WHERE Id IN:idList];
   //Loop for unchecking temp owner check box.
   for(Lead leadObj: leadList ){

       leadObj.TemporaryOwner__c = false;
       leadObj.OwnerId = OORTelesalesUtility.getQueueID(OORTelesalesTextConstant.OOR_TELE_CAMPAIGN_LEAD_QUEUE); 
       leadObj.OORTelesalesCampaign__c= NULL;
   } 
   try{
       update leadList;
       }
   catch(exception e){
   
       GeneralConstants.mailAnyExceptionToMe('Trigger : OORTelesalesAssociateCampaignOnLead','',e); 
   }
  }
}